package com.autotest.nore.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceVariableUtils {


    public static void main(String[] args) {
        System.out.println(handleVarible("${RandomData.arrayElement(abc::123::432)}"));

    }

    // 替换${}变量
    public static String handleVarible(String value) {
        if (value != null && value.contains("${") && value.contains("}")) {
            value = handleDollarPlaceholder(value);
        }
        return value;
    }

    public static String handleResBody(String value){
        if (value != null && value.contains("${") && value.contains("}")) {

        }
        return value;
    }

    public static String handleFunction(String value){
        String valueAfterReplace = value;
        String sub_var = value.trim();

        String[] array = sub_var.split("\\(");
        String[] classAndMethod = array[0].split("\\.");
        String clazz = classAndMethod[0];
        String method = classAndMethod[1];
        String[] paramsArray = null;

        if (array.length == 2) {
            String params = array[1].trim();
            if (!params.equals(")")) {
                params = params.substring(0, params.length() - 1);
                paramsArray = params.split(",");
            }
        }

        String replacementValue = getValueByReflect(clazz, method, paramsArray).toString();
//        valueAfterReplace = valueAfterReplace.replace(value, replacementValue);
        return  replacementValue;
    }

    // 处理${}
    public static String handleDollarPlaceholder(String value) {
        String valueAfterReplace = value;
        Pattern pattern = Pattern.compile("\\$\\{([^}]*)\\}");
        Matcher matcher = pattern.matcher(valueAfterReplace);

        while (matcher.find()) {
            String var = matcher.group();
            // 去掉${}
            String sub_var = var.substring(2, var.length() - 1).trim();

            String[] array = sub_var.split("\\(");
            String[] classAndMethod = array[0].split("\\.");
            String clazz = classAndMethod[0];
            String method = classAndMethod[1];
            String[] paramsArray = null;

            if (array.length == 2) {
                String params = array[1].trim();
                if (!params.equals(")")) {
                    params = params.substring(0, params.length() - 1);
                    paramsArray = params.split(",");
                }
            }

            String replacementValue = getValueByReflect(clazz, method, paramsArray).toString();
            valueAfterReplace = valueAfterReplace.replace(var, replacementValue);
        }

        return valueAfterReplace;
    }


    public static Object getValueByReflect(String stringOfClazz, String stringOfMethod, String[] params) {
        Class[] arrayParamClass = null;
        int lenOfParams = 0;

        if (params != null) {
            lenOfParams = params.length;
            arrayParamClass = new Class[lenOfParams];
            for (int i = 0; i < lenOfParams; i++) {
                arrayParamClass[i] = String.class;
            }
        }

        Class<?> clazz = null;

        try {
            clazz = Class.forName("com.autotest.nore.common.data." + stringOfClazz);

            Object ins = null;

            Method method = clazz.getMethod(stringOfMethod, arrayParamClass);
            Object obj = null;

            if (ins != null) {
                obj = method.invoke(ins, params);
            } else {
                obj = method.invoke(clazz.newInstance(), params);
            }

            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 处理 JSON 字符串中的占位符
    public static String processJsonWithVariables(String jsonContent) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        HeadersList headersList = objectMapper.readValue(jsonContent, HeadersList.class);

        for (Header header : headersList.getHeaders()) {
            header.setValue(handleVarible(header.getValue()));
        }

        return objectMapper.writeValueAsString(headersList);
    }
}